package com.nd.mobile.study.view;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.domain.VideoRecord;
import com.nd.mobile.study.view.adapter.SimpleListAdapter;

public class VideoRecordCatalogActivity extends BaseActivity{

	private ListView mLV; 
	private VideoRecordListAdapter mAdapter; 
	
	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.simple_list);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		loadList();
	}

	@Override
	protected void initViews() {
		mLV = (ListView)findViewById(R.id.common_lv);
	}
	
	private void loadList() {
		mLoading.p();
		new GetVideoRecordTask(this).execute();
	}
	
	private class GetVideoRecordTask extends SimpleAsyncTask<Void, Void, VideoRecord[]> {

		public GetVideoRecordTask(Context context) {
			super(context);
		}

		@Override
		protected VideoRecord[] onLoad(Void... params) throws Exception {
			return App.getInstance().getUser().getVideoRecord();
		}

		@Override
		protected void onUI(VideoRecord[] result) {
			mAdapter = new VideoRecordListAdapter(mContext, result);
			mLV.setAdapter(mAdapter);
			mAdapter.notifyDataSetChanged();
		}
	}
	
	private class VideoRecordListAdapter extends SimpleListAdapter<VideoRecord>{

		public VideoRecordListAdapter(Context context, List<VideoRecord> data) {
			super(context, data);
		}

		public VideoRecordListAdapter(Context context, VideoRecord[] data) {
			super(context, data);
		}
		
		@Override
		public boolean isEnabled(int position) {
			return false;
		}

		private class ViewHolder {
			TextView	tvTitle;
			TextView	tvBeginTime;
			TextView	tvEndTime;
			TextView	tvLVTime;
			TextView	tvLookNum;
			TextView    tvLVTimeTotal;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(null == convertView){
				convertView = mInflater.inflate(R.layout.video_record_list_item, null);
				
				holder = new ViewHolder();
				holder.tvTitle = (TextView) convertView.findViewById(R.id.video_record_catalog_title);
				holder.tvBeginTime = (TextView) convertView.findViewById(R.id.video_record_catalog_begin_time);
				holder.tvEndTime = (TextView) convertView.findViewById(R.id.video_record_catalog_end_time);
				holder.tvLVTime = (TextView) convertView.findViewById(R.id.video_record_catalog_lv_time);
				holder.tvLookNum = (TextView) convertView.findViewById(R.id.video_record_catalog_look_num);
				holder.tvLVTimeTotal = (TextView) convertView.findViewById(R.id.video_record_catalog_lv_time_total);
				
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tvTitle.setText(mData.get(position).getName());
			holder.tvBeginTime.setText("开始:"+mData.get(position).getBeginTime());
			holder.tvEndTime.setText("结束:"+mData.get(position).getEndTime());
			holder.tvLVTime.setText("看视频的时间(秒):"+mData.get(position).getLvTime());
			holder.tvLookNum.setText("看视频的次数:"+mData.get(position).getLookNum());
			holder.tvLVTimeTotal.setText("看视频的总时间(秒):"+mData.get(position).getLvTimeTotal());
			
			return convertView;
		}
	}
}
